package com.ecarx.library_base.base_widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.ecarx.library_base.R;
import com.ecarx.library_base.base_view.BToast;

public class InputDialog extends Dialog implements View.OnClickListener {
    private static final String TAG = "InputDialog";
    private ClickFinish clickFinish;
    private TextView text_name;

    private EditText message_edit;
    private String hint;

    private TextView cancel_button;

    private TextView confirm_button;

    private String text_nameString = null;

    private String capabilities;

    public final static int nameType = 1;// input name control
    public final static int passwordType = 2;// input password control
    private int type;//name or password

    private Context mContext;

    public InputDialog(@NonNull Context context, String hint, String text_nameString, ClickFinish clickFinish, int type) {
        super(context, R.style.dialog_pwd);
        this.text_nameString = text_nameString;
        this.clickFinish = clickFinish;
        this.type = type;
        this.hint = hint;
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(mContext).inflate(R.layout.input_dialog, null);
        setContentView(view);
        initView(view);
        text_name.setText(text_nameString);
        message_edit.setText(hint);
        initListener();
    }


    private void initListener() {
        cancel_button.setOnClickListener(this);
        confirm_button.setOnClickListener(this);
    }

    private void initView(View view) {
        text_name = (TextView) view.findViewById(R.id.wifi_title);
        message_edit = (EditText) view.findViewById(R.id.message_edit);
        cancel_button = (TextView) view.findViewById(R.id.cancel_button);
        confirm_button = (TextView) view.findViewById(R.id.confirm_button);
        switch (type) {
            case nameType:
                message_edit.setHint(R.string.please_input_name);
                break;
            case passwordType:
                message_edit.setHint(R.string.please_input_pswd);
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.confirm_button) {
            String inputmsg = message_edit.getText().toString();
            if (type == passwordType) {
                if (TextUtils.isEmpty(inputmsg)) {
                    BToast.showToast(getContext(), R.string.please_input_pswd);
                    return;
                }

                if (inputmsg.length() < 8) {
                    BToast.showToast(getContext(), R.string.password_length_limit);
                    return;
                }
            } else if (type == nameType) {
                if (TextUtils.isEmpty(inputmsg)) {
                    BToast.showToast(getContext(), R.string.please_input_name);
                    return;
                }
                Log.d(TAG, "nameType 2");
                if (inputmsg.length() < 1 || inputmsg.length() > 32) {
                    BToast.showToast(getContext(), R.string.name_length_limit);
                    return;
                }
                Log.d(TAG, "nameType 3");
            }
            if (clickFinish != null) {
                Log.d(TAG, "nameType 4");
                clickFinish.getInputMessage(inputmsg);
            }
            dismiss();
        } else if (id == R.id.cancel_button) {
            dismiss();
        }
    }

    public interface ClickFinish {
        void getInputMessage(String message);
    }
}
